package com.example.viwer3d.model;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Cubo extends Object3D{
	private FloatBuffer vertexBuffer;  // Buffer for vertex-array
	   private int numFaces = 6;
	   
	   private float[][] colors = {  // Colors of the 6 faces
	      {1.0f, 0.5f, 0.0f, 1.0f},  // 0. orange
	      {0.7f, 0.0f, 0.5f, 1.0f},  // 1. violet
	      {0.0f, 1.0f, 0.0f, 1.0f},  // 2. green
	      {0.0f, 0.0f, 1.0f, 1.0f},  // 3. blue
	      {1.0f, 0.0f, 0.0f, 1.0f},  // 4. red
	      {1.0f, 1.0f, 0.0f, 1.0f}   // 5. yellow
	   };
	  
	   private float[] vertices = {  // Vertices of the 6 faces
	      // FRENTE
	      -1.0f, -1.0f,  1.0f,  // 0. left-bottom-front
	       1.0f, -1.0f,  1.0f,  // 1. right-bottom-front
	      -1.0f,  1.0f,  1.0f,  // 2. left-top-front
	       1.0f,  1.0f,  1.0f,  // 3. right-top-front
	      // COSTAS
	       1.0f, -1.0f, -1.0f,  // 6. right-bottom-back
	      -1.0f, -1.0f, -1.0f,  // 4. left-bottom-back
	       1.0f,  1.0f, -1.0f,  // 7. right-top-back
	      -1.0f,  1.0f, -1.0f,  // 5. left-top-back
	      // ESQUERDA
	      -1.0f, -1.0f, -1.0f,  // 4. left-bottom-back
	      -1.0f, -1.0f,  1.0f,  // 0. left-bottom-front 
	      -1.0f,  1.0f, -1.0f,  // 5. left-top-back
	      -1.0f,  1.0f,  1.0f,  // 2. left-top-front
	      // DIREIRA
	       1.0f, -1.0f,  1.0f,  // 1. right-bottom-front
	       1.0f, -1.0f, -1.0f,  // 6. right-bottom-back
	       1.0f,  1.0f,  1.0f,  // 3. right-top-front
	       1.0f,  1.0f, -1.0f,  // 7. right-top-back
	      // TOPO
	      -1.0f,  1.0f,  1.0f,  // 2. left-top-front
	       1.0f,  1.0f,  1.0f,  // 3. right-top-front
	      -1.0f,  1.0f, -1.0f,  // 5. left-top-back
	       1.0f,  1.0f, -1.0f,  // 7. right-top-back
	      // BASE
	      -1.0f, -1.0f, -1.0f,  // 4. left-bottom-back
	       1.0f, -1.0f, -1.0f,  // 6. right-bottom-back
	      -1.0f, -1.0f,  1.0f, // 0. left-bottom-front
	       1.0f, -1.0f,  1.0f   // 1. right-bottom-front
	   };
	        
	   // Constructor - Set up the buffers
	   public Cubo() {
	      // Setup vertex-array buffer. Vertices in float. An float has 4 bytes
	      ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
	      vbb.order(ByteOrder.nativeOrder()); // Use native byte order
	      vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
	      vertexBuffer.put(vertices);         // Copy data into buffer
	      vertexBuffer.position(0);           // Rewind
	      maiorMedida= 2f;// todo objeto tem esse atributo (vide classe Object3D).
	      
	      this.nome = "Cubo";
	   }
	  
	   // Draw the shape
	   public void draw(GL10 gl) {
	      gl.glFrontFace(GL10.GL_CCW);    // Front face in counter-clockwise orientation
	      gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
	      gl.glCullFace(GL10.GL_BACK);    // Cull the back face (don't display)
	  
	      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
	      gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

	      // Render all the faces
	      for (int face = 0; face < numFaces; face++) {
	         // Set the color for each of the faces
	         gl.glColor4f(colors[face][0], colors[face][1], colors[face][2], colors[face][3]);
	         // Draw the primitive from the vertex-array directly
	         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, face*4, 4);
	      }
	      gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	      gl.glDisable(GL10.GL_CULL_FACE);
	   }
	   
}
